package com.quarkus.redis.service;

import io.quarkus.redis.client.RedisClient;
import org.jboss.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Arrays;

@ApplicationScoped
public class QsRedisService {

    private static final Logger LOGGER = Logger.getLogger(QsRedisService.class);

    @Inject
    private RedisClient redisClient;

    //在Redis中初始化数据
    @PostConstruct
    void config() {
        LOGGER.info("在Redis中初始化数据");
        add("project1", "关于project1的情况描述");
        add("project2", "关于project2的情况描述");
    }

    //在Redis中给某主键赋值
    public void add(String key,String value) {
        if ( get(key) != null ) return ;
        redisClient.set(Arrays.asList(key.toString(), value));
    }

    //在Redis中删除某主键的值
    public void del(String key) {
        LOGGER.info("在Redis中删除数据"+key);
        redisClient.del(Arrays.asList(key));
    }

    //在Redis中给某主键修改内容
    public void update(String key, String value) {
        redisClient.getset(key,value);
    }

    //在Redis中获得某主键的值
    public String get(String key) {
        return redisClient.get(key)!=null ? redisClient.get(key).toString():null;
    }

}
